package com.example.cymjoe.huaweishopping.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.cymjoe.huaweishopping.R;
import com.example.cymjoe.huaweishopping.SPXQActivity;
import com.example.cymjoe.huaweishopping.WebVieww;
import com.example.cymjoe.huaweishopping.adapter.GDTP2Adapter;
import com.example.cymjoe.huaweishopping.adapter.GridView_ShouYeAdapter;
import com.example.cymjoe.huaweishopping.adapter.SYLVAdapter;
import com.example.cymjoe.huaweishopping.bean.SYGridBean;
import com.example.cymjoe.huaweishopping.widget.MyGridView;
import com.example.cymjoe.huaweishopping.widget.MyListView;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShouYeFragment extends Fragment {
    private List<SYGridBean.DataBean.WindowsListBean> data = new ArrayList<>();
    private GridView_ShouYeAdapter adapter;
    private MyGridView gridView;
    private Button sjdg;
    private Button tqpd;
    private TextView num;
    private TextView numAll;
    private GDTP2Adapter adapter2;
    private Button yddth;
    private Button yjhx;
    private Button ygm;
    private List<SYGridBean.DataBean.NewRegionListBean> newRegionList;
    private ViewPager vpp;
    private List<SYGridBean.DataBean.ScrollAdsBean> scrollAdsBeen;
    private List<String> img = new ArrayList<>();
    private List<String> prdlList = new ArrayList<>();
    private SYLVAdapter sylvAdapter;
    private List<SYGridBean.DataBean.NewRegionListBean.RegionContentBean> data3 = new ArrayList<>();
    private Timer timer;
    private List<SYGridBean.DataBean.NewRegionListBean.RegionContentBean.ProductListBean> data2 = new ArrayList<>();
    private ImageView imm;
    private MyListView listView;

    public ShouYeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_shou_ye, container, false);
        x.view().inject(getActivity());
        initView(view);

        String path = "http://mw.vmall.com/vmall/home/appIndexInfoNew.json?version=75";
        getData(path);
        listener();
        vpp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                String ss = Integer.toString(position + 1);
                num.setText(ss);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        timer = new Timer();
        try {
            timer();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return view;
    }

    private void listener() {
        ygm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent3 = new Intent(getContext(), WebVieww.class);
                intent3.putExtra("path", "https://hwid1.vmall.com/CAS/mobile/standard/wapLogin.html?service=http%3A%2F%2Fmw.vmall.com%2Faccount%2Fcaslogin%3Furl%3D%252Fpriority&loginChannel=26000005&reqClientType=26");
                intent3.putExtra("name", "优购码");
                startActivity(intent3);
            }
        });
        sjdg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(getContext(), WebVieww.class);
                intent2.putExtra("path", "http://m.vmall.com/product/guideLabel");
                intent2.putExtra("name", "手机导购");
                startActivity(intent2);
            }
        });
        tqpd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), WebVieww.class);
                intent.putExtra("path", "http://mw.vmall.com/member/privilege");
                intent.putExtra("name", "会员特权频道");
                startActivity(intent);
            }
        });
        yddth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent4 = new Intent(getContext(), WebVieww.class);
                intent4.putExtra("path", "http://msale.vmall.com/peijian.html");
                intent4.putExtra("name", "移动端特惠");
                startActivity(intent4);
            }
        });
        yjhx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(getContext(), WebVieww.class);
                intent1.putExtra("path", "http://channels.mobile.huishoubao.com/?pid=1057");
                intent1.putExtra("name", "以旧换新");
                startActivity(intent1);
            }
        });
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String ss = data.get(position).getWindowURL();
                Log.e("ss", ss);
                //http://mw.vmall.com/product/
                String str = new String("http://mw.vmall.com/product/");
                String h = ss.replace(str, "");
                String[] split = h.split("\\.");
                String s = split[0];
                Log.e("ssss", s);
                if (s.equals("http://asale")) {
                    Intent intent = new Intent(getContext(), WebVieww.class);
                    intent.putExtra("path", ss);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(getContext(), SPXQActivity.class);


                    intent.putExtra("prdId", s);


                    startActivity(intent);
                }
            }
        });
    }

    private void initView(View view) {
        listView = ((MyListView) view.findViewById(R.id.lv));
        imm = ((ImageView) view.findViewById(R.id.imm));
        num = ((TextView) view.findViewById(R.id.num));
        numAll = ((TextView) view.findViewById(R.id.numAll));
        gridView = ((MyGridView) view.findViewById(R.id.gridView));
        sjdg = ((Button) view.findViewById(R.id.sjdg));
        tqpd = ((Button) view.findViewById(R.id.tqpd));
        yddth = ((Button) view.findViewById(R.id.yddth));
        yjhx = ((Button) view.findViewById(R.id.yjhx));
        ygm = ((Button) view.findViewById(R.id.ygm));
        vpp = ((ViewPager) view.findViewById(R.id.vpp));
    }


    private void getData(String path) {
        RequestParams params = new RequestParams(path);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                SYGridBean bean = gson.fromJson(result, SYGridBean.class);
                data = bean.getData().getWindowsList();
                scrollAdsBeen = bean.getData().getScrollAdsBeen();
                Log.e("ooo", scrollAdsBeen.get(0).getAdPicUrl());

                for (int i = 0; i < scrollAdsBeen.size() - 1; i++) {
                    img.add(scrollAdsBeen.get(i).getAdPicUrl());
                    prdlList.add(scrollAdsBeen.get(i).getAdPrdUrl());
                }
                newRegionList = bean.getData().getNewRegionList();

                for (int i = 0; i < newRegionList.size() - 1; i++) {
                    data3.add(newRegionList.get(i).getRegionContent());

                }
                for (int i = 0; i < data3.size() - 1; i++) {
                    data2 = data3.get(i).getProductList();

                }
                if (sylvAdapter == null) {
                    sylvAdapter = new SYLVAdapter(getContext(), newRegionList, data2);
                    listView.setAdapter(sylvAdapter);
                } else {
                    sylvAdapter.notifyDataSetChanged();
                }
                vpp.setOffscreenPageLimit(scrollAdsBeen.size() - 1);
                String ss = Integer.toString(img.size());
                numAll.setText("/" + ss);
                adapter2 = new GDTP2Adapter(getContext(), prdlList, img);
                vpp.setAdapter(adapter2);
                vpp.setCurrentItem(0);


                if (adapter == null) {
                    adapter = new GridView_ShouYeAdapter(data, getContext());
                    gridView.setAdapter(adapter);
                } else {
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    private void timer() {

        try {
            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    getActivity().runOnUiThread(new Runnable() {
                        // 主线程
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            int currentItem = vpp.getCurrentItem(); // 得到ViewPager当前显示第几页

                            int count = adapter.getCount(); // 总共有多少条

                            // 0123 4
                            if (currentItem == (count - 1)) {
                                // 如果使用第二个参数，单条显示Fragment数据可能无法加载！

                                vpp.setCurrentItem(0);
                                num.setText("1");
                            } else {
                                currentItem++;

                                vpp.setCurrentItem(currentItem);
                            }
                        }
                    });
                }
            }, 4000, 4000);
//            vpp.setOnTouchListener(new View.OnTouchListener() {
//                @Override
//                public boolean onTouch(View v, MotionEvent event) {
//                    int action = event.getAction();
//
//                    switch (action) {
//                        case MotionEvent.ACTION_DOWN:
//                            //按下
//                            //停止定时器
//                            Log.e("fff","ffff");
//                            timer.cancel(); //关闭
//                            break;
//
//                        case MotionEvent.ACTION_UP:
//                            //抬起
//                            //开启定时器
//                            Log.e("fff","aaaaa");
//                            timer = new Timer(); //新创建一个对象
//                            timer.schedule(new TimerTask() {
//
//                                @Override
//                                public void run() {
//                                    // TODO Auto-generated method stub
//
//                                    getActivity().runOnUiThread(new Runnable() {
//                                        // 主线程
//                                        @Override
//                                        public void run() {
//                                            // TODO Auto-generated method stub
//                                            int currentItem = vpp.getCurrentItem(); // 得到ViewPager当前显示第几页
//
//                                            int count = adapter.getCount(); // 总共有多少条
//
//                                            // 0123 4
//                                            if (currentItem == (count - 1)) {
//                                                // 如果使用第二个参数，单条显示Fragment数据可能无法加载！
//                                                vpp.setCurrentItem(0);
//                                                num.setText("1");
//                                            } else {
//                                                currentItem++;
//
//                                                vpp.setCurrentItem(currentItem);
//                                            }
//                                        }
//                                    });
//
//
//                                }
//                            }, 4000, 4000);
//
//                            break;
//                    }
//                    return false;
//                }
//            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}
